//
//  YCNetworkManager+Extensions.swift
//  WeiboDemo
//
//  Created by 尹成 on 2017/1/11.
//  Copyright © 2017年 yc. All rights reserved.
//

import Foundation

extension YCNetworkManager {
    
    /// 获取微博数据
    ///
    /// - Parameters:
    ///   - since_id: 若指定此参数,则返回比since_id大的微博,即最新微博(下拉刷新),默认0
    ///   - max_id: 若指定此参数,则返回比max_id小或相等的微博,即之前的(上拉加载),默认0
    ///   - completion: 回调函数
    func statusList(_ since_id:Int64 = 0 ,_ max_id:Int64 = 0 ,completion:@escaping (_ list:[[String:AnyObject]]?,_ isSuccess:Bool) -> ()) {
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let parames = ["since_id":"\(since_id)","max_id":"\(max_id > 0 ? max_id - 1 : 0)"]
        
        request(URLString: urlString, parameters: parames as [String : AnyObject]?) { (result, isSuccess) in
            let json = (result as? NSDictionary)?["statuses"] as? [[String : AnyObject]]
            completion(json,isSuccess)
        }
    }
    
    /// 获取微博未读数量
    func unreadCount(completion:@escaping (_ count:Int) -> ()) {
        
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlString = ""
        let params = ["uid":uid]
        
        request(URLString: urlString, parameters: params as [String : AnyObject]?) { (result, isSuccess) in
            let dict = result as? [String:AnyObject]
            let count = dict?["status"] as? Int
            //            print("未读数量：",result!)
            completion(count ?? 0)
        }
    }
    
}


// MARK: - 获取用户信息
extension YCNetworkManager{
    fileprivate func loadUserInfo(completion:@escaping (_ userInfo:[String:AnyObject])->()) -> () {
        
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        let params = ["uid":uid]
        request(URLString: urlString,
                parameters: params as [String : AnyObject]?) { (result, isSuccess) in
                    print("用户信息：",result as? [String:AnyObject] ?? [:])
                    completion(result as! [String : AnyObject])
        }
        
    }
}

// MARK: - OAuth 相关方法
extension YCNetworkManager{
    
    func loadAccessToken(code:String,completion:@escaping (Bool) -> ()) {
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let params = ["client_id":WeiboAppKey,
                      "client_secret":WeiboSecret,
                      "grant_type":"authorization_code",
                      "code":code,
                      "redirect_uri":WeiboRedirectURL]
        
        request(method: .POST, URLString: urlString, parameters: params as [String : AnyObject]?,needToken: false){ (result,isSuccess) in
//            print("请求token： ",result as? [String:AnyObject] ?? [:] )
            
            self.userAccount.yc_set(with: result,userInfo: nil)
            
            self.loadUserInfo(completion: { (userInfo) in
//                print("请求userInfo： ",userInfo)
                self.userAccount.yc_set(with: result, userInfo:userInfo)
                completion(true)
            })
            
        }
    }
}
